import util from "../../../libs/util.js";
import { getDescrible, machineList } from "../../../api/machine.js";
const state = {
  choiceSite: null,
  choiceDevice: null,
  choiceAccessory: null,
  curComponent: "detail1",
  active: 1, //1训练点 2头显 3外设 列表激活
  deviceActive: 1, //选中某个头显激活
  tableList: [
    // 主列表
  ],
  deviceList: [
    // 头显列表
  ],
  accessoryList: [], // 外设列表
};

const mutations = {
  table_list(state, list) {
    state.tableList = list;
    state.deviceList = [];
    state.accessoryList = [];
    state.choiceSite = null;
  },
  device_list(state, list) {
    state.deviceList = list;
    state.accessoryList = [];
    state.choiceDevice = null;
  },
  accessory_list(state, list) {
    state.accessoryList = list;
    state.choiceAccessory = null;
  },
  bind_unbind_device(state, { newSite, deviceID }) {
    // let curSite = state.tableList.find((site) => site.ID == newSite.ID);
    // if (curSite) {
    //   curSite.BindingNumber = newSite.BindingNumber;
    //   curSite.Devices = newSite.Devices;
    //   // curSite._highlight = true;
    // }
    if (deviceID) {
      let curDevice = state.deviceList.find((device) => device.ID == deviceID);
      curDevice.Bindings = null;
      // curDevice._highlight = true;
    }
  },
  bind_unbind_accessory(state, { newDevice, accessoryID }) {
    let curDevice = state.deviceList.find(
      (device) => device.ID == newDevice.ID
    );
    if (curDevice) {
      curDevice.Accessories = newDevice.Accessories;
    }
    if (accessoryID) {
      let curAcc = state.accessoryList.find((acc) => acc.ID == accessoryID);
      curAcc.Bindings = null;
      // curAcc._highlight = true;
    }
  },
  change_cur_component(state, type) {
    state.curComponent = "detail" + type;
    state.active = type;
  },
  choice_site(state, obj) {
    state.choiceSite = obj;
  },
  choice_device(state, obj) {
    state.choiceDevice = obj;
  },
  choice_accessory(state, { id }) {
    let acc = state.accessoryList.find((item) => item.ID == id);
    state.choiceAccessory = acc;
  },
  //头显激活
  device_active(state, id) {
    state.deviceActive = id;
  },
  //编辑Site Device Accessory
  edit(state, { id, Tag, Name, Type, type }) {
    let curObj = null;
    if (type == "Site") {
      curObj = state.tableList.find((item) => item.ID == id);
    } else if (type == "Device") {
      curObj = state.deviceList.find((item) => item.ID == id);
    } else {
      curObj = state.accessoryList.find((item) => item.ID == id);
    }
    if (curObj) {
      Name ? (curObj.Name = Name) : "";
      Tag ? (curObj.Tag = Tag) : "";
      Type ? (curObj.Type = Type) : "";
    }
  },
  on_SiteStateChange(state, { ID, State }) {
    let curSite = state.tableList.find((site) => site.ID == ID);
    if (curSite) {
      curSite.State = State;
    } else {
      //添加到tableList
    }
  },
  on_DeviceStateChange(state, { ID, State, BatteryLevel }) {
    let curDevice = state.deviceList.find((device) => device.ID == ID);
    if (curDevice) {
      let beforeState = curDevice.State;
      curDevice.State = State;
      curDevice.BatteryLevel = BatteryLevel;
      if (curDevice.Bindings) {
        let site = state.tableList.find(
          (item) => item.ID == curDevice.Bindings[0].ID
        );
        if (beforeState != State && site) {
          State == "Online"
            ? (site.BindingOnlineCount += 1)
            : (site.BindingOnlineCount -= 1);
        }
      }
    } else {
      //添加到deviceList
    }
  },
  on_AccessoryStateChange(state, { ID, State, BatteryLevel }) {
    let curAccess = state.accessoryList.find((access) => access.ID == ID);
    if (curAccess) {
      curAccess.State = State;
      curAccess.BatteryLevel = BatteryLevel;
    } else {
      //添加到accessoryList
    }
  },
};

const actions = {
  /**
   * 赋值 tableList
   * @param {Array} list
   * @param {Number} type  Site  训练点  Device 头显  Accessory  外设
   */
  getTableList({ commit }, { list, defaultRow, type }) {
    let _type = 0;
    list = list || [];
    if (type == "Site") {
      commit("table_list", list);
      _type = 1;
    } else if (type == "Device") {
      commit("device_list", list);
      _type = 2;
    } else {
      commit("accessory_list", list);
      _type = 3;
    }
    commit("change_cur_component", _type);
    // let site = sites[defaultRow]
    // if(site) {
    //     site._highlight = true;
    //     let devices = site.Devices || [];
    //     commit('device_list', devices);
    //     let device = devices[defaultRow];
    //     if(device) {
    //         commit('device_active', device.ID);
    //         let accessories = util.to(device);
    //         commit('accessory_list', accessories);
    //     }
    // }
  },
  /**
   * 赋值 deviceList
   * @param {Number} id 训练点ID
   * @param {Boolean} needUpdate 是否需要获取最新的 deviceList
   */
  getDeviceList({ commit, state }, { id, deviceID, needUpdate }) {
    commit("device_active", 0);
    // commit('choice_device', null);
    let site = state.tableList.find((item) => item.ID == id);
    if (needUpdate) {
      getDescrible({ Type: "Site", ID: id }).then((data) => {
        if (data.Code == 0) {
          // step 1 更新site device
          site = data.Data;
          commit("bind_unbind_device", { newSite: site, deviceID });
          // step 2 更新deviceList
          if (!deviceID) {
            let devices = site.Devices || [];
            commit("device_list", devices);
          }
          // step 3 更新choiceSite
          commit("choice_site", site);
        }
      });
    } else {
      let devices = site.Devices || [];
      commit("device_list", devices);
      commit("choice_site", site);
    }
  },
  /**
   * 赋值 accessoryList
   * @param {Number} id 头显ID
   */
  getAccessoryList({ commit, state }, { id, needUpdate, accessoryID }) {
    console.log('***************')
    commit("device_active", id);
    let device = state.deviceList.find((item) => item.ID == id);
    // if (needUpdate) {
      getDescrible({ Type: "Device", ID: device.ID }).then((data) => {
        if (data.Code == 0) {
          // step 1 更新deviceList
          device = data.Data;
          console.log(device,'=========')
          commit("bind_unbind_accessory", { newDevice: device, accessoryID });
          // step 2 更新accessoryList
          if (!accessoryID) {
            let accessories = util.to(device);
            commit("accessory_list", accessories);
          }
          // step 3 更新choiceDevice
          commit("choice_device", device);
        }
      });
    // } else {
    //   let accessories = util.to(device);
    //   commit("accessory_list", accessories);
    //   commit("choice_device", device);
    // }
  },
  SiteStateChange({ commit, state }, info) {
    console.log("on_SiteStateChange");
    commit("on_SiteStateChange", info);
  },
  DeviceStateChange({ commit, state }, info) {
    console.log("on_DeviceStateChange");
    commit("on_DeviceStateChange", info);
  },
  AccessoryStateChange({ commit, state }, info) {
    console.log("on_AccessoryStateChange");
    commit("on_AccessoryStateChange", info);
  },
};

const getters = {};

export default {
  state,
  mutations,
  actions,
  getters,
};
